#include "thresholddialog.h"
#include "ui_thresholddialog.h"

#include <QGridLayout>
#include <QHBoxLayout>
#include <QPainter>

ThresholdDialog::ThresholdDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ThresholdDialog)
{
    ui->setupUi(this);
    setWindowTitle(tr("Adjust the threshold"));
    setFixedSize(343, 332);
    setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint & ~Qt::WindowMinimizeButtonHint);
    setModal(true);

    connect(ui->thresholdSlider1, SIGNAL(valueChanged(int)), ui->thresholdValue1, SLOT(setValue(int)));
    connect(ui->thresholdValue1, SIGNAL(valueChanged(int)), ui->thresholdSlider1, SLOT(setValue(int)));
    connect(ui->thresholdSlider2, SIGNAL(valueChanged(int)), ui->thresholdValue2, SLOT(setValue(int)));
    connect(ui->thresholdValue2, SIGNAL(valueChanged(int)), ui->thresholdSlider2, SLOT(setValue(int)));
    connect(ui->thresholdSlider2, SIGNAL(valueChanged(int)), this, SLOT(updateThreshold2(int)));
    connect(ui->thresholdSlider1, SIGNAL(valueChanged(int)), this, SLOT(updateThreshold1(int)));
    connect(ui->applyButton, SIGNAL(clicked()), this, SLOT(applyButtonClicked()));
    connect(ui->cancelButton, SIGNAL(clicked()), this, SLOT(cancelButtonClicked()));
}

ThresholdDialog::~ThresholdDialog()
{
    delete ui;
}

void ThresholdDialog::setThreshold(int threshold)
{
    ui->thresholdValue2->setValue(threshold);
}

void ThresholdDialog::setGrayInfo(int* grayInfo)
{
    for(int i = 0; i < 256; i++)
    {
        m_grayInfo[i] = grayInfo[i];
    }
    m_histomgram = Histomgram(m_grayInfo, ui->thresholdValue2->value(), Qt::gray);
    QImage image = m_histomgram.getImage().scaledToHeight(182).scaledToWidth(246);
    ui->histomgramLabel->setPixmap(QPixmap::fromImage(image));
}

void ThresholdDialog::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.setPen(Qt::gray);
    painter.drawRect(10, 10, width() - 20, height() - 20);
}

void ThresholdDialog::closeEvent(QCloseEvent *)
{
    emit cancelOperator();
}

void ThresholdDialog::applyButtonClicked()
{
    QString str = tr("Adjust the threshold. ")
                + tr("Threshold1 : ") + QString::number(ui->thresholdSlider1->value())
                + tr(", Threshold2 : ") + QString::number(ui->thresholdSlider2->value()) + tr(".");
    emit applyOperator(str);
    close();
}

void ThresholdDialog::cancelButtonClicked()
{
    close();
}

void ThresholdDialog::updateThreshold1(int min)
{
    ui->thresholdSlider2->setMinimum(min);
    ui->thresholdValue2->setMinimum(min);

    m_histomgram.setThreshold1(min);
    m_histomgram.drawThreshold();
    QImage image = m_histomgram.getImage().scaledToHeight(182).scaledToWidth(246);
    ui->histomgramLabel->setPixmap(QPixmap::fromImage(image));

    emit threshold1Changed(min);
}

void ThresholdDialog::updateThreshold2(int max)
{
    ui->thresholdSlider1->setMaximum(max);
    ui->thresholdValue1->setMaximum(max);

    m_histomgram.setThreshold2(max);
    m_histomgram.drawThreshold();
    QImage image = m_histomgram.getImage().scaledToHeight(182).scaledToWidth(246);
    ui->histomgramLabel->setPixmap(QPixmap::fromImage(image));

    emit threshold2Changed(max);
}
